3 struct MapOpportunities: View {
8 let opportunities: [Opportunity]
10 let arrowheadSize = CGFloat(10.0)
13 ForEach(opportunities, id: \.id) { edge in
16 // First we transform edges from percentage to map coordinates
17 let origin = CGPoint(x: w(edge.origin.x), y: h(edge.origin.y))
18 let destination = CGPoint(x: w(edge.destination.x), y: h(edge.destination.y))
20 let multiplier = CGFloat(edge.destination.x > edge.origin.x ? 1.0 : -1.0)
21 let upperAngle = -CGFloat.pi / 4.0
22 let lowerAngle = CGFloat.pi / 4.0
24 let offsetOrigin = CGPoint(x: origin.x + multiplier * (vertexSize.width / 2.0), y: origin.y)
25 let offsetDestination = CGPoint(
26 x: destination.x - multiplier * (vertexSize.width / 2.0), y: destination.y)
28 path.move(to: offsetOrigin)
29 path.addLine(to: offsetDestination)
31 path.move(to: offsetDestination)
34 x: offsetDestination.x - multiplier * arrowheadSize * cos(upperAngle),
36 offsetDestination.y - multiplier * arrowheadSize * sin(upperAngle)))
38 path.move(to: offsetDestination)
41 x: offsetDestination.x - multiplier * arrowheadSize * cos(lowerAngle),
43 offsetDestination.y - multiplier * arrowheadSize * sin(lowerAngle)))
45 path.move(to: offsetDestination)
48 CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
49 ).strokedPath(StrokeStyle(lineWidth: lineWidth / 4, dash: [10.0])).stroke(Color.map.opportunityColor)
53 func h(_ dimension: CGFloat) -> CGFloat {
54 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
57 func w(_ dimension: CGFloat) -> CGFloat {
58 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
62 struct MapOpportunities_Previews: PreviewProvider {
63 static var previews: some View {
65 mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0,
66 vertexSize: CGSize(width: 25.0, height: 25.0),
68 Opportunity(id: 1, origin: CGPoint(x: 2.0, y: 34.0), destination: CGPoint(x: 23.0, y: 76.2))